package com.maszy.service;

import java.awt.List;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.maszy.bean.Action;
import com.maszy.db.DBConnection;

public class PosService {

	private static PreparedStatement pst;
	private static ResultSet rs = null;

	// 插入详细交易记录主表total表
	public int addTotal(String[] a) {
		DBConnection con = new DBConnection();
		int result = 0;
		String sql = "INSERT INTO [xiahe].[dbo].[TrTotal]([SaleNo], [EcrID], [EmployeeID], [DSale], [DiscountRate], [AmtDisCount], [AddRate], [AmtAdd], [AmtSum], [AmtRec], [AmtRtn], [MemberID], [CusAgeLayer], [AmtMemSum], [StockID], [DCreate], [DModify], [RoomNo], [ItemCnt], [RoomPSale], [AmtCounteract], [MainStatus], [SplitFlag], [Notes], [Rpt_ID], [TableNo], [Flag], [LsNo], [ManNum], [RtnManager], [oldMemCunu], [MemCumu], [StaFlag], [AuthManager], [InvoiceNo]) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			pst = con.getConnection().prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (a.length == 35) {
				for (int i = 0; i < a.length; i++) {
					pst.setObject(i + 1, a[i]);
				}
			}
			result = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return result;
	}

	// 插入详细交易记录Item表
	public int addItem(String[] a) {
		DBConnection con = new DBConnection();
		int result = 0;
		String sql ="INSERT INTO [xiahe].[dbo].[TrItem]([SaleNo], [ItemNo], [BCode], [CommName], [PSale], [PTSale], [CUnit], [DisCountRate], [Qty], [POriginal], [PMemberSale], [AmtSum], [DisCountSum], [CommID], [FlgTB], [FlgCalMemberScore], [FlgBatch], [SaleTaxRate], [ItemAmtSaleTax], [FlgSaleRtn], [InvUnit], [PCSType], [InvQty], [MemberRate], [QRUDiscount], [BatchNo], [Status], [ConvRate], [CommTypeID], [FlgCalMemberRateToType], [BgnConsumeTime], [EndConsumeTime], [MaxOverConsume], [CommStatus], [FlgCounteract], [CounterActRate], [CommGroup], [Notes], [SalerNo], [OperateFlag], [AuthManger], [TicketAmt], [TaxRateAmt], [exprice], [examtsum])VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		
		try {
			pst = con.getConnection().prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (a.length == 45) {
				for (int i = 0; i < a.length; i++) {
					if("False".equals(a[i].trim())){
						a[i]="1";
					}else if("True".equals(a[i].trim())){
						a[i]="0";
					}
					pst.setObject(i + 1, a[i]);
				}
			}
			result = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return result;
	}

	// 插入详细交易记录Pay表
	public int addPay(String[] a) {
		DBConnection con = new DBConnection();
		int result = 0;
		String sql ="INSERT INTO [xiahe].[dbo].[TrPay]([SaleNo], [ItemNo], [PosPayID], [PayName], [CardNo], [PayCur], [CurID], [CurName], [CurRate], [AmtPay], [PayNotes])VALUES(?,?,?,?,?,?,?,?,?,?,?)";
		try {
			pst = con.getConnection().prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (a.length == 11) {
				for (int i = 0; i < a.length; i++) {
					pst.setObject(i + 1, a[i]);
				}
			}
			result = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return result;
	}
	// 插入会员卡RFID交易记录
	public int addRfid(String[] a) {
		DBConnection con = new DBConnection();
		int result = 0;
		String sql ="INSERT INTO [xiahe].[dbo].[Rfid]([sSN], [SerialNo], [Status], [SaleDate], [EcrId], [sClerkId], [ReceiptNo], [cardNo1], [cardNo2], [inputAmt], [operateAmt], [remainAmt], [allPayAmt], [cardSerialNo], [CardTypeId], [CardTypeName], [BatchNo], [Flagerr],[DoorStore_Id]) " +
				"SELECT ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,a.Maszy_DoorStore_Id FROM Maszy_CashRegister a where a.Maszy_CashRegister_NO=?";
		System.out.println(sql);
		try {
			pst = con.getConnection().prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			
			if (a.length == 18) {
				for (int i = 0; i < a.length; i++) {
					pst.setObject(i + 1, a[i]);
					System.out.print("'"+a[i]+"',");
					
				}
				System.out.println();
				pst.setObject(19,a[4]);
			}
			result = pst.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return result;
	}
}
